<template>
    <div id="gtable">
        <el-table :data="this.$store.getters.mylist.tableList" style="width: 100%" :row-style="rowStyle" :cell-style="cellStyle">
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" class="demo-table-expand">
                        <el-form-item label="进价">
                            <span>{{ props.row.inprice }}</span>
                        </el-form-item>
                        <el-form-item label="限购数">
                            <span>{{ props.row.maxNum }}</span>
                        </el-form-item>
                        <el-form-item label="今日已售">
                            <span>{{ props.row.sellToday }}</span>
                        </el-form-item>
                        <el-form-item label="总销售">
                            <span>{{ props.row.sellAll }}</span>
                        </el-form-item>
                        <el-form-item label="今日浏览量">
                            <span>{{ props.row.view }}</span>
                        </el-form-item>
                        <el-form-item label="总浏览量">
                            <span>{{ props.row.allView }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column label="商品图">
                <template slot-scope="scope">
                    <img :src="scope.row.goodsImg">
                </template>
            </el-table-column>
            <el-table-column prop="goodsId" label="商品ID">
            </el-table-column>
            <el-table-column prop="goodsName" label="商品名">
            </el-table-column>
            <el-table-column prop="price" label="售价">
            </el-table-column>
            <el-table-column label="上架状态">
                <template slot-scope="scope">
                    <span>{{scope.row.state}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="num" label="库存">
            </el-table-column>
            <el-table-column prop="goodsType" label="商品类别">
            </el-table-column>
            <el-table-column label="操作" width='145px'>
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" :plain="true" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
            <el-form :model="form" :rules="rules" ref="formName">
                <el-form-item label="商品价格" :label-width="formLabelWidth" prop="price">
                    <el-input v-model.number="form.price" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="商品状态:" :label-width="formLabelWidth" prop="state">
                    <el-radio-group v-model="form.state">
                        <el-radio :label="true">已上架</el-radio>
                        <el-radio :label="false">未上架</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="商品数量:" :label-width="formLabelWidth" prop="num">
                    <el-input v-model.number="form.num"></el-input>
                </el-form-item>
                <el-form-item label="商品类型:" :label-width="formLabelWidth" prop="type">
                    <el-select v-model="form.type" placeholder="请选择商品类型">
                        <el-option label="书桌" value="shanghai"></el-option>
                        <el-option label="沙发" value="beijing"></el-option>
                        <el-option label="双人床" value="beijing"></el-option>
                        <el-option label="台灯" value="beijing"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editOk">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapActions } from 'vuex'
import * as url from '../../api/ygr_goods_api.js'
export default {
    name: 'GTable',
    data() {
        return {
            dialogFormVisible: false,
            index:0,
            form: {
                price: '',
                state: true,
                num: '',
                type: ''
            },
            rules: {
                price: [
                    { required: true, message: '价格不能为空', trigger: 'blur' },
                    { type: 'number', message: '价格必须为数字值', trigger: 'blur' }
                ],
                num: [
                    { required: true, message: '数量不能为空', trigger: 'blur' },
                    { type: 'number', message: '数量必须为数字值', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '请选择商品类别', trigger: 'change' }
                ],
                state: [
                    { required: true, message: '请选择上架状态', trigger: 'change' }
                ]
            },
            formLabelWidth: '120px'
        }
    },
    methods: {
        ...mapActions(['getData','changePage']),
        handleEdit(index, row) { //修改编辑
            console.log(index, row);
            this.dialogFormVisible = true;//修改弹出框显示
            //将修改行的数据显示填充弹出框内容区
            this.form.price=row.price;
            this.form.state=row.state;
            this.form.num=row.num;
            this.form.type=row.goodsType;
            //将修改行在总数据的索引值记录
            this.index=(this.$store.state.goodsStore.now-1)*4+index;
            console.log(this.index)
        },
        editOk(){//确定修改
          this.$refs.formName.validate((valid) => {
                if (valid) {
                  this.dialogFormVisible=false;//关闭弹出框
                  //由于测试接口不准确，将数据存入vuex（正常请求服务器数据库刷新）
                  console.log(this.$store.state.goodsStore.allList[this.index].price)
                  this.$store.state.goodsStore.allList[this.index].price=this.form.price;
                  this.$store.state.goodsStore.allList[this.index].state=this.form.state;
                  this.$store.state.goodsStore.allList[this.index].num=this.form.num;
                  this.$store.state.goodsStore.allList[this.index].goodsType=this.form.type;
                  this.changePage(this.$store.getters.mylist.now)

                } else {
                    console.log('error submit!!');
                    this.$message('无法修改')
                    return false;
                }
            });

        },
        handleDelete(index, row) { //删除
            console.log(index, row);
            this.$axios.get(url.DELGOODS)
                .then(res => {
                    console.log(res)
                    console.log(res.data.data.message)
                    const h = this.$createElement;
                    this.$message({
                        message: h('p', null, [
                            h('span', null, '通知： '),
                            h('i', { style: 'color: teal' }, res.data.data.message)
                        ])
                    });
                    if (res.data.code == 200) {
                        this.getData()
                    }
                })

        },
        cellStyle() {
            return "text-align:center;background: rgba(255,255,255,.3);"
        },
        rowStyle() {
            return "text-align:center;background: rgba(255,255,255,.3);"
        }
    },
    created() {
        this.getData()
    }
}
</script>
<style scoped>
.el-table {
    color: black;
    text-align: center;
    background-color: rgba(255, 255, 255, .3);
}

img {
    width: 60px;
    height: 60px;
}

thead th {
    color: black;
    background: transparent;

}

tr {
    background: transparent;
}

.demo-table-expand {
    font-size: 0;
}

.demo-table-expand label {
    width: 90px;
    color: #99a9bf;
}

.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
}
</style>